﻿using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Security;
using WindowsAPI.Service;

namespace WindowsAPI
{
    [SuppressUnmanagedCodeSecurity, Dll(FileName)]
    public static class Ole32
    {
        public const string FileName = "ole32.dll";

        [DllImport(FileName, CharSet = CharSet.Auto)]
        public static extern int OleGetClipboard(ref IDataObject data);

        [DllImport(FileName, CharSet = CharSet.Auto)]
        public static extern int OleSetClipboard(IDataObject pDataObj);

        [DllImport(FileName, CharSet = CharSet.Auto)]
        public static extern int OleFlushClipboard();

        [DllImport(FileName)]
        public static extern int ReadClassStg(HandleRef pStg, [In, Out] ref Guid pclsid);

        [DllImport(FileName, CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern void CoTaskMemFree(IntPtr pv);

        [DllImport(FileName, PreserveSig = false)]
        public static extern IClassFactory2 CoGetClassObject([In] ref Guid clsid, int dwContext, int serverInfo, [In] ref Guid refiid);

        [DllImport(FileName, PreserveSig = false)]
        [return: MarshalAs(UnmanagedType.Interface)]
        public static extern object CoCreateInstance([In] ref Guid clsid, [MarshalAs(UnmanagedType.Interface)] object punkOuter, int context, [In] ref Guid iid);

    }

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("B196B28F-BAB4-101A-B69C-00AA00341D07")]
    [ComImport]
    public interface IClassFactory2
    {
        void CreateInstance([MarshalAs(UnmanagedType.Interface), In] object unused, [In] ref Guid refiid, [MarshalAs(UnmanagedType.LPArray), Out] object[] ppunk);

        void LockServer(int fLock);

        void GetLicInfo([Out] tagLICINFO licInfo);

        void RequestLicKey([MarshalAs(UnmanagedType.U4), In] int dwReserved, [MarshalAs(UnmanagedType.LPArray), Out] string[] pBstrKey);

        void CreateInstanceLic([MarshalAs(UnmanagedType.Interface), In] object pUnkOuter, [MarshalAs(UnmanagedType.Interface), In] object pUnkReserved, [In] ref Guid riid, [MarshalAs(UnmanagedType.BStr), In] string bstrKey, [MarshalAs(UnmanagedType.Interface)] out object ppVal);
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagLICINFO
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cbLicInfo = Marshal.SizeOf(typeof(tagLICINFO));
        public int fRuntimeAvailable;
        public int fLicVerified;
    }
}